package cn.xuchunh.guava.string;

import com.google.common.base.*;

import java.lang.reflect.Field;
import java.util.Arrays;

/**
 * Created on 2017/4/3.
 *
 * <p><a href="http://wiki.jikexueyuan.com/project/google-guava-official-tutorial/strings.html">字符串处理：分割，连接，填充</a></p>
 *
 * @author XCH
 */
public class StringOperation {

    public static void main(String[] args) {
        System.out.println(Joiner.on(",").skipNulls().join("Harry", null, "Ron", "Hermione"));
        System.out.println(Joiner.on(',').skipNulls().join("Harry", null, "Ron", "Hermione"));
        System.out.println(Joiner.on(",").useForNull("null").join("Harry", null, "Ron", "Hermione"));
        System.out.println(Joiner.on(",").skipNulls().join(new String[]{"Harry", null, "Ron", "Hermione"}));
        System.out.println(Joiner.on(",").skipNulls().join(Arrays.asList("Harry", null, "Ron", "Hermione")));

        String splitTarget = "foo,bar,,   qux";
        System.out.println(Splitter.on(",").trimResults().omitEmptyStrings().split(splitTarget));
        System.out.println(Joiner.on(" : ").join(Splitter.on(",").trimResults().omitEmptyStrings().limit(2).split(splitTarget)));

        System.out.println("\n");
        String s = "Tom4\tNancy435000\n\t\t4444Vit5or555";
        System.out.println(s);
        System.out.println(CharMatcher.JAVA_ISO_CONTROL.removeFrom(s));
        System.out.println(CharMatcher.DIGIT.retainFrom(s));
        // 去除两端的空格，并把中间的连续空格替换成单个空格
        System.out.println(CharMatcher.WHITESPACE.trimAndCollapseFrom(s, ' '));
        System.out.println(CharMatcher.WHITESPACE.collapseFrom(s, '+'));
        System.out.println(CharMatcher.JAVA_DIGIT.replaceFrom(s, '*'));
        System.out.println(CharMatcher.DIGIT.replaceFrom(s, '-'));
        System.out.println(CharMatcher.JAVA_DIGIT.or(CharMatcher.JAVA_LOWER_CASE).retainFrom(s));
        System.out.println(CharMatcher.anyOf("an").retainFrom(s));
        System.out.println(CharMatcher.is('4').retainFrom(s));
        System.out.println(CharMatcher.inRange('a', 'z').retainFrom(s));

        System.out.println("\n");
        printFields(CharMatcher.class.getFields());


        System.out.println("\n");
        printFields(Charsets.class.getFields());

        System.out.println("\n");
        System.out.println(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "CONSTANT_NAME"));
        printFields(CaseFormat.class.getFields());

    }

    private static void printFields(Field[] fields) {
        for (Field field : fields) {
            System.out.println(field.getName());
        }
    }


}
